//
//  Tab1ViewController.m
//  SITABLE
//
//  Created by songjun on 16/9/18.
//  Copyright © 2016年 Sita. All rights reserved.
//

#import "Tab1ViewController.h"
#define  HdearViewH 49.0
#define CellH SCREEN_HEIGHT/3
#define isBindCars @"0"
@interface Tab1ViewController ()

@end

@implementation Tab1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedStringFromTable(@"Trip", @"InfoPlist", nil);
    
    baseTableview.showsVerticalScrollIndicator = NO;
    baseTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    baseTableview.backgroundColor = [UIColor whiteColor];
    
    staticDaysArr = [[NSMutableArray alloc] initWithCapacity:1];
    //staticMonthArr = [[NSMutableArray alloc] initWithCapacity:1];
    historyArr = [[NSMutableArray alloc] initWithCapacity:1];
    _tripsArr = [[NSMutableArray alloc] initWithCapacity:1];
    
    NSDictionary *trip1Dic = [NSDictionary dictionaryWithObjectsAndKeys:@"开始时间: 2016-09-08 09:12:34",@"start",@"结束时间: 2016-09-08 09:22:12",@"end",@"里程: 12km",@"mile",@"平均速度: 39km/h",@"avSpeed" ,nil];
    
     NSDictionary *trip2Dic = [NSDictionary dictionaryWithObjectsAndKeys:@"开始时间: 2016-09-07 19:11:04",@"start",@"结束时间: 2016-09-07 19:19:11",@"end",@"里程: 5km",@"mile",@"平均速度: 32km/h",@"avSpeed" ,nil];
    
     NSDictionary *trip3Dic = [NSDictionary dictionaryWithObjectsAndKeys:@"开始时间: 2016-09-06 13:10:07",@"start",@"结束时间: 2016-09-06 13:19:07",@"end",@"里程: 8km",@"mile",@"平均速度: 24km/h",@"avSpeed" ,nil];
    
     NSDictionary *trip4Dic = [NSDictionary dictionaryWithObjectsAndKeys:@"开始时间: 2016-09-05 09:09:31",@"start",@"结束时间 :2016-09-05 09:16:21",@"end",@"里程: 5km",@"mile",@"平均速度: 21km/h",@"avSpeed" ,nil];
    
     NSDictionary *trip5Dic = [NSDictionary dictionaryWithObjectsAndKeys:@"开始时间: 2016-09-04 16:18:35",@"start",@"结束时间: 2016-09-04 16:34:21",@"end",@"里程: 15km",@"mile",@"平均速度: 24km/h",@"avSpeed" ,nil];
     [historyArr addObject:trip1Dic];
     [historyArr addObject:trip2Dic];
     [historyArr addObject:trip3Dic];
     [historyArr addObject:trip4Dic];
     [historyArr addObject:trip5Dic];

    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dateComponents = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:[NSDate date]];
    currentYear =  dateComponents.year;
    currentMonth = dateComponents.month;
    currentDay =  dateComponents.day;
    
    NSDate *date1 = [NSDate date];//给定的时间
    NSDate *lastDay1 = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date1];
    NSDateFormatter *formatterTest = [[NSDateFormatter alloc] init];
    [formatterTest setDateFormat:@"YYYY年MM月dd日"];
    beforeDateStr = [formatterTest stringFromDate:lastDay1];
    
    //[self weekdayStringFromDate:[NSDate date]]
    NSString *beforWeekDat = [self weekdayStringFromDate:lastDay1];
    beforeDateStr = [beforeDateStr stringByAppendingString:beforWeekDat];
    
    //获取前10个月
    //staticMonthArr = [self monthYadea];
    
    
    //获取前10天
    staticDaysArr=[self daysYadea];
    self.segIndex = 0;
    
   
    
    self.view.backgroundColor = UIColorHexRGBA(linBoardBGGrayColor, 1.0);
    baseTableview.backgroundColor =UIColorHexRGBA(linBoardBGGrayColor, 1.0);
    
    
    //添加上啦下拉刷新
    baseTableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.segIndex==0) {
            [baseTableview.mj_header endRefreshing];
             [baseTableview.mj_header endRefreshing];
        }else{
            [self requestTrips];
        }
        
    }];
    
    
    baseTableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.segIndex==0) {
            [baseTableview.mj_footer endRefreshing];
            [baseTableview.mj_header endRefreshing];
        }else{
            [self requestTripsPull];
        }
    }];
}

-(void)requestTripsPull
{
    MBProgressHUD *mbhud = [[MBProgressHUD alloc] initWithView:self.view];
    mbhud.mode = MBProgressHUDModeText;
    mbhud.tag = 3024;
    [self.view addSubview:mbhud];
    mbhud.labelText = @"努力加载中......";
    [mbhud show:YES];
    
    //linboardPushFresh
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setTimeoutInterval:10.0f];
    NSString *mUrl = [NSString stringWithFormat:@"%@%@",linboardBaseUrl,linboardPullFresh];
    NSString *pageN = [NSString stringWithFormat:@"%d",pageNumber];
    NSDictionary *parametersDic = [NSDictionary dictionaryWithObjectsAndKeys:linboardBodaoid,@"bodaoid",LinBoardPagesize,@"pagesize",pageN,@"pagenumber",nil];
    //NSLog(@"%@",parametersDic);
    //行程保存方法,每条行程基本信息为:PallaTrip,保存至数据库,行程的zip文件以tripid命名保存至沙盒便于存取
    [manager POST:mUrl parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        ;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *errorcode = [responseObject valueForKey:@"errorCode"];
        if ([errorcode isEqualToString:@"SUCCESS"]) {
            NSArray *infArr = [responseObject valueForKey:@"data"];
            //NSLog(@"****:%@",infArr);
            //需要过滤掉那些已经缓存的路径
            //创建数据库测试
            LKDBHelper *globalHelper = [Utilization getUsingLKDBHelper];
            NSString *sqlString = [NSString stringWithFormat:@"select * from PallaTrip where bodaoId = %@",linboardBodaoid];
            
            NSArray * dbsearchResult = [globalHelper searchWithSQL:sqlString toClass:[PallaTrip class]];
            NSMutableDictionary *lessDic = [[NSMutableDictionary alloc] initWithCapacity:1];
            
            for (int i = 0; i < infArr.count; i++) {
                NSDictionary *tripDic = [infArr objectAtIndex:i];
                NSString *tripId = [NSString stringWithFormat:@"%lld",[[tripDic valueForKey:@"tripId"] longLongValue]];
                [lessDic setValue:tripDic forKey:tripId];
                for (int j= 0; j < dbsearchResult.count; j++) {
                    PallaTrip *testDb = [dbsearchResult objectAtIndex:j];
                    if ([testDb.tripId isEqualToString:tripId]) {
                        [lessDic setValue:testDb forKey:testDb.tripId];
                    }else{
                        
                    }
                }
            }
            
            NSMutableArray *distincArr = [NSMutableArray arrayWithCapacity:1];
            NSArray *fullArr = [lessDic allValues];
            for (int j = 0 ; j<fullArr.count; j++) {
                if ([[fullArr objectAtIndex:j] isKindOfClass:[PallaTrip class]]) {
                    
                }else{
                    [distincArr addObject:[fullArr objectAtIndex:j]];
                }
            }
            
            //去重完成:
            if (distincArr.count !=0) {
                 pageNumber++;
                [self saveTripsToDb:distincArr];
            }else{
                NSString *sqlString = [NSString stringWithFormat:@"select * from PallaTrip where bodaoId = %@%@%@ order by begintime desc",@"'",linboardBodaoid,@"'"];
                LKDBHelper *globalHelper = [Utilization getUsingLKDBHelper];
                NSArray * dbsearchResult = [globalHelper searchWithSQL:sqlString toClass:[PallaTrip class]];
                if (dbsearchResult!=0) {
                    _tripsArr = [NSMutableArray arrayWithArray:dbsearchResult];
                    [baseTableview reloadData];
                    //NSLog(@"***:%ld",_tripsArr.count);
                }
            }
            [baseTableview.mj_footer endRefreshing];
            [baseTableview.mj_header endRefreshing];
   
        }
              [mbhud removeFromSuperview];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //        NSData *dicdata=[[error userInfo] valueForKey:@"com.alamofire.serialization.response.error.data"];
        //        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:dicdata options:NSJSONReadingMutableContainers error:nil];
        //        NSLog(@"****:%@",dic);
        
        [baseTableview.mj_footer endRefreshing];
        [baseTableview.mj_header endRefreshing];
        mbhud.labelText = @"行程获取失败!";
        [self performSelector:@selector(dismissHud1302) withObject:nil afterDelay:1.5f];
        ;
    }];

    
}


-(void)requestTrips
{
    MBProgressHUD *mbhud = [[MBProgressHUD alloc] initWithView:self.view];
    mbhud.mode = MBProgressHUDModeText;
    mbhud.tag = 3024;
    [self.view addSubview:mbhud];
    mbhud.labelText = @"努力加载中......";
    [mbhud show:YES];
    
    //linboardPushFresh
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setTimeoutInterval:10.0f];
    NSString *mUrl = [NSString stringWithFormat:@"%@%@",linboardBaseUrl,linboardPushFresh];
    
    NSDictionary *parametersDic = [NSDictionary dictionaryWithObjectsAndKeys:linboardBodaoid,@"bodaoid",LinBoardPagesize,@"pagesize",@"0",@"pagenumber",nil];
    
    //行程保存方法,每条行程基本信息为:PallaTrip,保存至数据库,行程的zip文件以tripid命名保存至沙盒便于存取
    [manager POST:mUrl parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        ;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *errorcode = [responseObject valueForKey:@"errorCode"];
        if ([errorcode isEqualToString:@"SUCCESS"]) {
            NSArray *infArr = [responseObject valueForKey:@"data"];
            //NSLog(@"****:%d",infArr.count);
            //需要过滤掉那些已经缓存的路径
            //创建数据库测试
            LKDBHelper *globalHelper = [Utilization getUsingLKDBHelper];
            NSString *sqlString = [NSString stringWithFormat:@"select * from PallaTrip where bodaoId = %@",linboardBodaoid];
            
            NSArray * dbsearchResult = [globalHelper searchWithSQL:sqlString toClass:[PallaTrip class]];
            NSMutableDictionary *lessDic = [[NSMutableDictionary alloc] initWithCapacity:1];
            
            for (int i = 0; i < infArr.count; i++) {
                NSDictionary *tripDic = [infArr objectAtIndex:i];
                NSString *tripId = [NSString stringWithFormat:@"%lld",[[tripDic valueForKey:@"tripId"] longLongValue]];
                [lessDic setValue:tripDic forKey:tripId];
                for (int j= 0; j < dbsearchResult.count; j++) {
                    PallaTrip *testDb = [dbsearchResult objectAtIndex:j];
                    if ([testDb.tripId isEqualToString:tripId]) {
                        [lessDic setValue:testDb forKey:testDb.tripId];
                    }else{
                        
                    }
                }
            }
            
            NSMutableArray *distincArr = [NSMutableArray arrayWithCapacity:1];
            NSArray *fullArr = [lessDic allValues];
            for (int j = 0 ; j<fullArr.count; j++) {
                if ([[fullArr objectAtIndex:j] isKindOfClass:[PallaTrip class]]) {
                    
                }else{
                    [distincArr addObject:[fullArr objectAtIndex:j]];
                }
            }
            
            //去重完成:
            if (distincArr.count !=0) {
                pageNumber++;
                [self saveTripsToDb:distincArr];
            }else{
                [baseTableview.mj_footer endRefreshing];
                [baseTableview.mj_header endRefreshing];
                NSString *sqlString = [NSString stringWithFormat:@"select * from PallaTrip where bodaoId = %@%@%@ order by begintime desc",@"'",linboardBodaoid,@"'"];
                LKDBHelper *globalHelper = [Utilization getUsingLKDBHelper];
                NSArray * dbsearchResult = [globalHelper searchWithSQL:sqlString toClass:[PallaTrip class]];
                if (dbsearchResult!=0) {
                    _tripsArr = [NSMutableArray arrayWithArray:dbsearchResult];
                    [baseTableview reloadData];
                    //NSLog(@"***:%ld",_tripsArr.count);
                }
            }
            
        }
        [mbhud removeFromSuperview];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [baseTableview.mj_footer endRefreshing];
        [baseTableview.mj_header endRefreshing];
        //        NSData *dicdata=[[error userInfo] valueForKey:@"com.alamofire.serialization.response.error.data"];
        //        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:dicdata options:NSJSONReadingMutableContainers error:nil];
        //        NSLog(@"****:%@",dic);
        mbhud.labelText = @"行程获取失败!";
        [self performSelector:@selector(dismissHud1302) withObject:nil afterDelay:1.5f];
        ;
    }];
}


-(void)saveTripsToDb:(NSArray *)tripsArr
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    //linboardBodaoid
    NSString *accountIdString = [[NSUserDefaults standardUserDefaults] valueForKey:@"pallaAccountId"];
    
    NSString *jsonFolder = [path stringByAppendingPathComponent:accountIdString];
    
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    if (![[NSFileManager defaultManager] fileExistsAtPath:jsonFolder]) {
        [fileManager createDirectoryAtPath:jsonFolder withIntermediateDirectories:YES attributes:nil error:nil];
    }

    //先下载保存zip,成功后保存数据库
    for (int i = 0; i < tripsArr.count; i++)
    {
        NSDictionary *tripDic = [tripsArr objectAtIndex:i];
        NSString *tripId = [NSString stringWithFormat:@"%lld",[[tripDic valueForKey:@"tripId"] longLongValue]];
        
        NSString *routefile = [NSString stringWithFormat:@"%@",[tripDic valueForKey:@"routefile"]];
        
        NSURL *url = [NSURL URLWithString:routefile];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
//        NSError *error = nil;
//        NSData *data = [NSURLConnection sendSynchronousRequest:request
//                                             returningResponse:nil
//                                                         error:&error];
       [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
           
           if (data != nil){
               NSString *zipPath = [NSString stringWithFormat:@"%@/%@.zip", jsonFolder, tripId];
               
               if ([data writeToFile:zipPath atomically:YES])
               {
                   oldArchive = [ZZArchive archiveWithURL:[NSURL fileURLWithPath:zipPath]
                                                    error:nil];
                   
                   firstArchiveEntry = oldArchive.entries[0];
                   
                   NSData *newData = [firstArchiveEntry newDataWithError:nil];
                   
                   if (newData.length!=0) {
                       id jsonObj = [NSJSONSerialization JSONObjectWithData:newData options:NSJSONReadingMutableContainers error:nil];
                       
                       if (jsonObj != nil)
                       {
                           
                           //创建数据库测试
                           LKDBHelper *globalHelper = [Utilization getUsingLKDBHelper];
                           NSString * lineSingleQuoteSQL = [NSString stringWithFormat:@"%@%@%@", @"'", tripId, @"'"];
                           NSArray * dbsearchResult = [globalHelper search:[PallaTrip class] where:[NSString stringWithFormat:@"tripId=%@",lineSingleQuoteSQL] orderBy:0 offset:0 count:100];
                           if (dbsearchResult.count == 0)
                           {
                               PallaTrip *userTrip = [PallaTrip mj_objectWithKeyValues:tripDic];
                               PallaTrip *newUserTrip = [[PallaTrip alloc] init];
                               newUserTrip.tripId = userTrip.tripId;
                               newUserTrip.lat = userTrip.lat;
                               newUserTrip.lng = userTrip.lng;
                               newUserTrip.begintime = userTrip.begintime;
                               newUserTrip.endtime = userTrip.endtime;
                               newUserTrip.distance = userTrip.distance;
                               newUserTrip.speed = userTrip.speed;
                               newUserTrip.soc = userTrip.soc;
                               newUserTrip.routefile = userTrip.routefile;
                               newUserTrip.begintimestr = userTrip.begintimestr;
                               newUserTrip.endtimestr = userTrip.endtimestr;
                               newUserTrip.bodaoId = linboardBodaoid;
                               [newUserTrip saveToDB];
                           }else{
                           }
                       }
                   }
               }
           }

           
            ;
        }];
        
    
            }
    
    NSString *bikeCodeString =linboardBodaoid;
    
    //PallaTrip
    if (bikeCodeString.length == 0) {
        return;
    }
    NSString *sqlString = [NSString stringWithFormat:@"select * from PallaTrip where bodaoId = %@%@%@ order by begintime desc",@"'",bikeCodeString,@"'"];
    ///NSLog(@"%@",sqlString);
    LKDBHelper *globalHelper = [Utilization getUsingLKDBHelper];
    NSArray * dbsearchResult = [globalHelper searchWithSQL:sqlString toClass:[PallaTrip class]];
    _tripsArr = [NSMutableArray arrayWithArray:dbsearchResult];
    [baseTableview reloadData];
    
    [baseTableview.mj_footer endRefreshing];
    [baseTableview.mj_header endRefreshing];
    //NSLog(@"哈哈哈");
    
    
}


-(void)dismissHud1302
{
    MBProgressHUD *hud = [self.view viewWithTag:3024];
    if (hud) {
        [hud removeFromSuperview];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH,HdearViewH);
    headerView.backgroundColor = UIColorHexRGBA(linBoardBGGrayColor, 1.0);
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"日汇总",@"历史",nil]];
    //seg.backgroundColor = UIColorHexRGBA(linBoardGrayColor, 1.0);
    seg.tintColor = UIColorHexRGBA(linBoardNavitagionColor, 1.0);
    
    NSDictionary *textDic = [NSDictionary dictionaryWithObjectsAndKeys:
                             [[UIColor lightGrayColor]       colorWithAlphaComponent:1.0f],UITextAttributeTextColor,
                             [UIFont boldSystemFontOfSize:14],UITextAttributeFont,nil];
    
    [seg setTitleTextAttributes:textDic forState:UIControlStateNormal];
    
    [seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    seg.selectedSegmentIndex = 0;
    switch (self.segIndex) {
        case 0:
        {
            seg.selectedSegmentIndex = 0;
        }
            break;
        case 1:
        {
            seg.selectedSegmentIndex = 1;
        }
            break;
        case 2:
        {
            seg.selectedSegmentIndex = 2;
        }
            break;
        default:
            break;
    }
    
    
    int gap =5;
    seg.frame = CGRectMake(gap,gap, SCREEN_WIDTH-2*gap, HdearViewH-2*gap);
    [headerView addSubview:seg];
    return headerView;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.segIndex==0) {
        return CellH;
    }else{
        return 160;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.segIndex==0) {
        return 4;
    }else{
        return _tripsArr.count;
    }
    return 0;
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return HdearViewH;
}



#pragma mark -----------------
- (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}


#pragma mark ----------获取10天的数组
-(NSMutableArray *)daysYadea
{
    NSMutableArray *daysArr = [[NSMutableArray alloc] initWithCapacity:1];
    int day = (int)currentDay-1;
    int curYear = (int)currentYear;
    int curMonth = (int)currentMonth-1;
    for (int i = 0; i<9; i++) {
        if (day==1) {
            if (currentMonth == 1) {
                curYear = (int)currentYear-1;
                curMonth  = 12;
            }
            day = (int)[self daysInMonth:curMonth forYear:curYear];
        }
        day--;
        [daysArr addObject:[NSNumber numberWithInt:day]];
    }
    [daysArr insertObject:[NSNumber numberWithInt:(int)currentDay-1] atIndex:0];
    
    NSMutableArray *reverseArr = [[NSMutableArray alloc] initWithCapacity:1];
    for (int i = (int)daysArr.count-1; i>=0; i--) {
        [reverseArr addObject:[NSNumber numberWithInt:[[daysArr objectAtIndex:i] intValue]]];
    }
    return reverseArr;
    return 0;
}

-(NSUInteger)daysInMonth:(NSUInteger)month forYear:(NSUInteger)year{
    
    NSUInteger daysInMonth = 31;
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            daysInMonth = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            daysInMonth = 30;
            break;
            
        case 2:
        {
            BOOL isLeapYear = [self isLeapYear:year];
            if (isLeapYear == YES) {
                daysInMonth = 29;
            }else{
                daysInMonth = 28;
            }
        }
            break;
            
        default:
            break;
    }
    return daysInMonth;
}


-(BOOL)isLeapYear:(NSUInteger)year {
    if ((year%4==0 && year%100!=0) || year%400==0) {
        return YES;
    }else {
        return NO;
    }
    return NO;
}


#pragma mark ------------获取10个月的数组
-(NSMutableArray *)monthYadea
{
    NSMutableArray *mothArr = [[NSMutableArray alloc] initWithCapacity:1];
    int month = (int)currentMonth-1;
    for (int i = 0; i<9; i++) {
        if (month==1) {
            month = 13;
        }
        month--;
        [mothArr addObject:[NSNumber numberWithInt:month]];
    }
    [mothArr insertObject:[NSNumber numberWithInt:(int)currentMonth-1] atIndex:0];
    NSMutableArray *reverseArr = [[NSMutableArray alloc] initWithCapacity:1];
    for (int i = (int)mothArr.count-1; i>=0; i--) {
        [reverseArr addObject:[NSNumber numberWithInt:[[mothArr objectAtIndex:i] intValue]]];
    }
    return reverseArr;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell==nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    //NSLog(@"****:%d,%ld",self.segIndex,(long)indexPath.row);
    if ((self.segIndex==0)&&indexPath.row == 0)
    {
        StatisticsHeaderViewTableViewCell *staticCell = [[[NSBundle mainBundle] loadNibNamed:@"StatisticsHeaderViewTableViewCell" owner:self options:nil] lastObject];
        staticCell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.segIndex == 0) {
            //显示月
            NSInteger beforMonth = currentMonth-1;
            NSInteger beforYear = currentYear;
            if (beforMonth == 0) {
                beforMonth = 12;
                beforYear = currentYear-1;
            }
            staticCell.dateLabel.text = [NSString stringWithFormat:@"%ld年%ld月",(long)beforYear,(long)beforMonth];
                   }
        if (self.segIndex == 0) {
#pragma mark ------显示日
            staticCell.dateLabel.text = beforeDateStr;

             if ([isBindCars isEqualToString:@"1"])
            {
                staticCell.totalMilesLabel.text = [NSString stringWithFormat:@"%@km",self.vertiCalDaysDic[@"mileage_last_day10"]];
                NSString *time = self.vertiCalDaysDic[@"times_last_day10"];
                NSInteger min = [time integerValue]/60000;
                if (min%60000>=30000)
                {
                    min = min+1;
                }
                staticCell.totalTimeLabel.text =[NSString stringWithFormat:@"%ld分钟",(long)min];
                
                NSString *perC = @"%";
                staticCell.haodianLabel.text =[NSString stringWithFormat:@"%@%@",self.vertiCalDaysDic[@"oilwear_last_day10"],perC];
                staticCell.averageSpeedLabel.text =  [NSString stringWithFormat:@"%@km/h",self.vertiCalDaysDic[@"speed_last_day10"]];
                int totalMileInt1111 =[[self.daysArr objectAtIndex:0] intValue];
                int totoTimeInt=[[self.daysArr objectAtIndex:1] intValue];
                
                double jPaiCount = 0;
                
                if (totoTimeInt!=0) {
                    jPaiCount=totalMileInt1111*0.7+(totalMileInt1111*totalMileInt1111/totoTimeInt)*2;
                }
                staticCell.jianpaiLabel.text = [NSString stringWithFormat:@"%.1fm³",jPaiCount];
                staticCell.jianpaiLabel.text = @"第1";
            }
        }
        return staticCell;
    }else{
        //下面的统计表cell
        if (self.segIndex==0) {
            Cell2TableViewCell *staticCell = [[[NSBundle mainBundle] loadNibNamed:@"Cell2TableViewCell" owner:self options:nil] lastObject];
//            staticCell.bgLabel.layer.cornerRadius = 8;
//            staticCell.bgLabel.clipsToBounds = YES;
            staticCell.selectionStyle = UITableViewCellSelectionStyleNone;
            switch (indexPath.row) {
                case 1:
                {
                    staticCell.titttleLabel.text = @"里程计数表";
                    staticCell.typeLabel.text = @"km";
                    //staticCell.bgIv.image = [UIImage imageNamed:@"statistic0"];
                    
                    FSLineChart *altitudeLineChart = [[FSLineChart alloc] initWithFrame:CGRectMake(40, 50, SCREEN_WIDTH-60, SCREEN_HEIGHT/3-80)];
                    altitudeLineChart.tag = 101;
                    altitudeLineChart.valueLabelBackgroundColor = [UIColor clearColor];
                    altitudeLineChart.backgroundColor = [UIColor clearColor];
                    
                    altitudeLineChart.fillColor = UIColorHexRGBA(linBoardWiteColor, 0.5);
                    altitudeLineChart.color =UIColorHexRGBA(linBoardWiteColor, 0.9);

                    altitudeLineChart.innerGridColor =[UIColor colorWithWhite:1.0 alpha:0.5];
                    altitudeLineChart.indexLabelTextColor = [UIColor whiteColor];
                    altitudeLineChart.valueLabelTextColor =[UIColor whiteColor];
                    
                    altitudeLineChart.valueLabelBackgroundColor = [UIColor clearColor];
                    altitudeLineChart.backgroundColor = [UIColor clearColor];
                    
                    [staticCell.baseView addSubview:altitudeLineChart];
                    
                    NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:10];
                    //日 油耗表曲线
                    if (self.segIndex==0)
                    {
                        for(int i=0;i<10;i++) {
                            int r = (rand() + rand()) % 10;
                            chartData[i] = [NSNumber numberWithInt:r + 40];
                        }
                        
                        
                    }
#pragma mark ------日  里程计数表
                    if (self.segIndex==0)
                    {
                        if ([isBindCars isEqualToString:@"0"])
                        {
                            for(int i=0;i<10;i++) {
                                int r = (rand() + rand()) % 10;
                                chartData[i] = [NSNumber numberWithInt:20+r];
                            }
                        }
                        else if ([isBindCars isEqualToString:@"1"])
                        {
                            NSString *mile1Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day01"];
                            chartData[0] = [NSNumber numberWithFloat:[mile1Month intValue]*0.001];
                            
                            NSString *mile2Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day02"];
                            chartData[1] = [NSNumber numberWithFloat:[mile2Month intValue]*0.001];
                            
                            NSString *mile3Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day03"];
                            chartData[2] = [NSNumber numberWithFloat:[mile3Month intValue]*0.001];
                            
                            NSString *mile4Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day04"];
                            chartData[3] = [NSNumber numberWithFloat:[mile4Month intValue]*0.001];
                            
                            NSString *mile5Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day05"];
                            chartData[4] = [NSNumber numberWithFloat:[mile5Month intValue]*0.001];
                            
                            NSString *mile6Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day06"];
                            chartData[5] = [NSNumber numberWithFloat:[mile6Month intValue]*0.001];
                            
                            NSString *mile7Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day07"];
                            chartData[6] = [NSNumber numberWithFloat:[mile7Month intValue]*0.001];
                            
                            NSString *mile8Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day08"];
                            chartData[7] = [NSNumber numberWithFloat:[mile8Month intValue]*0.001];
                            
                            NSString *mile9Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day09"];
                            chartData[8] = [NSNumber numberWithFloat:[mile9Month intValue]*0.001];
                            
                            NSString *mile10Month = [self.vertiCalDaysDic valueForKey:@"mileage_last_day10"];
                            chartData[9] = [NSNumber numberWithFloat:[mile10Month intValue]*0.001];
                        }
                    }
                    
                    altitudeLineChart.verticalGridStep = 10;
                    altitudeLineChart.horizontalGridStep = 9;
                    altitudeLineChart.valueLabelPosition = ValueLabelLeft;
                    altitudeLineChart.labelForIndex = ^(NSUInteger item) {
                        switch (item) {
                            case 0:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                         return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:0] intValue]];
                                    }
                                        break;
                                    default:
                                        break;
                                }
                                
                                
                            }
                                break;
                            case 1:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:1] intValue]];
                                    }
                                        break;
                                    default:
                                        break;
                                }
                                
                            }
                                break;
                            case 2:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                         return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:2] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 3:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:3] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 4:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                       return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:4] intValue]];
                                    }
                                        break;
                                    default:
                                        break;
                                }
                            }
                                break;
                            case 5:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:5] intValue]];
                                    }
                                        break;
                                    default:
                                        break;
                                }
                                
                            }
                                break;
                            case 6:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                           return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:6] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 7:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:7] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 8:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:8] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 9:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:9] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                                
                            default:
                                break;
                        }
                        return @"yadea";
                    };
                    
                    altitudeLineChart.labelForValue = ^(CGFloat value) {
                        
                        return [NSString stringWithFormat:@"%.f", value];
                    };
                    [altitudeLineChart setChartData:chartData];
    
                }
                    break;
                    
                case 2:
                {
                    staticCell.titttleLabel.text = @"速度计数表";
                    staticCell.typeLabel.text = @"km/h";
                    //staticCell.bgIv.image = [UIImage imageNamed:@"statistic1"];
                    
                    FSLineChart *altitudeLineChart = [[FSLineChart alloc] initWithFrame:CGRectMake(40, 50, SCREEN_WIDTH-60, SCREEN_HEIGHT/3-80)];
                    altitudeLineChart.tag = 102;
                    altitudeLineChart.valueLabelBackgroundColor = [UIColor clearColor];
                    altitudeLineChart.backgroundColor = [UIColor clearColor];
                    
                    altitudeLineChart.fillColor = UIColorHexRGBA(linBoardWiteColor, 0.5);//[UIColor colorWithWhite:1.0 alpha:0.5];
                    altitudeLineChart.color =UIColorHexRGBA(linBoardWiteColor, 0.9);
                    altitudeLineChart.innerGridColor =[UIColor colorWithWhite:1.0 alpha:0.5];
                    altitudeLineChart.indexLabelTextColor = [UIColor whiteColor];
                    altitudeLineChart.valueLabelTextColor =[UIColor whiteColor];
                    
                    altitudeLineChart.valueLabelBackgroundColor = [UIColor clearColor];
                    altitudeLineChart.backgroundColor = [UIColor clearColor];
                    
                    [staticCell.baseView addSubview:altitudeLineChart];
                    
                    
                    
                    NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:10];
                    
                    if (self.segIndex==0)
                    {
                        chartData[0]= [NSNumber numberWithUnsignedInt:1];
                        chartData[1]= [NSNumber numberWithUnsignedInt:3];
                        chartData[2]= [NSNumber numberWithUnsignedInt:2];
                        chartData[3]= [NSNumber numberWithUnsignedInt:3];
                        chartData[4]= [NSNumber numberWithUnsignedInt:3];
                        chartData[5]= [NSNumber numberWithUnsignedInt:1];
                        chartData[6]= [NSNumber numberWithUnsignedInt:2];
                        chartData[7]= [NSNumber numberWithUnsignedInt:2];
                        chartData[8]= [NSNumber numberWithUnsignedInt:3];
                        chartData[9]= [NSNumber numberWithUnsignedInt:2];
                        
                        
                        
                        
                    }
#pragma mark ------日   速度计数表
                    if (self.segIndex==0)
                    {
                        if ([isBindCars isEqualToString:@"0"])
                        {
                            for(int i=0;i<10;i++) {
                                int r = (rand() + rand()) % 10;
                                chartData[i] = [NSNumber numberWithInt:20+r];
                            }
                        }
                        else if ([isBindCars isEqualToString:@"1"])
                        {
                            NSString *
                            mile1Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day01"];
                            chartData[0] = [NSNumber numberWithFloat:[mile1Month floatValue]*0.01];
                            
                            
                            NSString *mile2Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day02"];
                            chartData[1] = [NSNumber numberWithFloat:[mile2Month floatValue]*0.01];
                            
                            NSString *mile3Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day03"];
                            chartData[2] = [NSNumber numberWithFloat:[mile3Month floatValue]*0.01];
                            
                            NSString *mile4Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day04"];
                            chartData[3] = [NSNumber numberWithFloat:[mile4Month floatValue]*0.01];
                            
                            NSString *mile5Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day05"];
                            chartData[4] = [NSNumber numberWithFloat:[mile5Month floatValue]*0.01];
                            
                            NSString *mile6Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day06"];
                            chartData[5] = [NSNumber numberWithFloat:[mile6Month floatValue]*0.01];
                            
                            NSString *mile7Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day07"];
                            chartData[6] = [NSNumber numberWithFloat:[mile7Month floatValue]*0.01];
                            
                            NSString *mile8Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day08"];
                            chartData[7] = [NSNumber numberWithFloat:[mile8Month floatValue]*0.01];
                            
                            NSString *mile9Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day09"];
                            chartData[8] = [NSNumber numberWithFloat:[mile9Month floatValue]*0.01];
                            
                            NSString *mile10Month = [self.vertiCalDaysDic valueForKey:@"speed_last_day10"];
                            chartData[9] = [NSNumber numberWithFloat:[mile10Month floatValue]*0.01];
                        }
                    }
                    
                    altitudeLineChart.verticalGridStep = 10;
                    altitudeLineChart.horizontalGridStep = 9;
                    altitudeLineChart.valueLabelPosition = ValueLabelLeft;
                    altitudeLineChart.labelForIndex = ^(NSUInteger item) {
                        switch (item) {
                            case 0:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                         return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:0] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                                
                            }
                                break;
                            case 1:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                         return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:1] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 2:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                         return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:2] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 3:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:3] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 4:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:4] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                            }
                                break;
                            case 5:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:5] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 6:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                         return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:6] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 7:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:7] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 8:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                         return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:8] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 9:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                          return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:9] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                                
                            default:
                                break;
                        }
                        return @"yadea";
                    };
                    
                    altitudeLineChart.labelForValue = ^(CGFloat value) {
                        return [NSString stringWithFormat:@"%.f", value];
                    };
                    
                    [altitudeLineChart setChartData:chartData];
                    
                }
                    break;
                    
                case 3:
                {
                    staticCell.titttleLabel.text = @"油耗计数表";
                    staticCell.typeLabel.text = @"%";
                    //staticCell.bgIv.image = [UIImage imageNamed:@"statistic3"];
                    
                    FSLineChart *altitudeLineChart = [[FSLineChart alloc] initWithFrame:CGRectMake(40, 50, SCREEN_WIDTH-60, SCREEN_HEIGHT/3-80)];
                    altitudeLineChart.tag = 102;
                    altitudeLineChart.valueLabelBackgroundColor = [UIColor clearColor];
                    altitudeLineChart.backgroundColor = [UIColor clearColor];
                    
                    altitudeLineChart.fillColor = UIColorHexRGBA(linBoardWiteColor, 0.5);//[UIColor colorWithWhite:1.0 alpha:0.5];
                    altitudeLineChart.color =UIColorHexRGBA(linBoardWiteColor, 0.9);
                    altitudeLineChart.innerGridColor =[UIColor colorWithWhite:1.0 alpha:0.5];
                    altitudeLineChart.indexLabelTextColor = [UIColor whiteColor];
                    altitudeLineChart.valueLabelTextColor =[UIColor whiteColor];
                    
                    altitudeLineChart.valueLabelBackgroundColor = [UIColor clearColor];
                    altitudeLineChart.backgroundColor = [UIColor clearColor];
                    
                    [staticCell.baseView addSubview:altitudeLineChart];
                    
                    
                    
                    NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:10];
                    
                    if (self.segIndex==0)
                    {
                        for(int i=0;i<10;i++) {
                            int r = (rand() + rand()) % 10;
                            chartData[i] = [NSNumber numberWithInt:r + 40];
                        }
                        
                        
                    }
#pragma mark ------日    油耗计数表
                    if (self.segIndex==0)
                    {
                        if ([isBindCars isEqualToString:@"0"])
                        {
                            for(int i=0;i<10;i++) {
                                int r = (rand() + rand()) % 10;
                                chartData[i] = [NSNumber numberWithInt:20+r];
                            }
                        }
                        else if ([isBindCars isEqualToString:@"1"])
                        {
                            NSString *mile1Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day01"];
                            chartData[0] = [NSNumber numberWithInt:[mile1Month floatValue]];
                            
                            NSString *mile2Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day02"];
                            chartData[1] = [NSNumber numberWithInt:[mile2Month intValue]];
                            
                            NSString *mile3Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day03"];
                            chartData[2] = [NSNumber numberWithInt:[mile3Month intValue]];
                            
                            NSString *mile4Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day04"];
                            chartData[3] = [NSNumber numberWithInt:[mile4Month intValue]];
                            
                            NSString *mile5Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day05"];
                            chartData[4] = [NSNumber numberWithInt:[mile5Month intValue]];
                            
                            NSString *mile6Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day06"];
                            chartData[5] = [NSNumber numberWithInt:[mile6Month intValue]];
                            
                            NSString *mile7Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day07"];
                            chartData[6] = [NSNumber numberWithInt:[mile7Month intValue]];
                            
                            NSString *mile8Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day08"];
                            chartData[7] = [NSNumber numberWithInt:[mile8Month intValue]];
                            
                            NSString *mile9Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day09"];
                            chartData[8] = [NSNumber numberWithInt:[mile9Month intValue]];
                            
                            NSString *mile10Month = [self.vertiCalDaysDic valueForKey:@"oilwear_last_day10"];
                            chartData[9] = [NSNumber numberWithInt:[mile10Month intValue]];
                        }
                    }
                    
                    
                    altitudeLineChart.verticalGridStep = 10;
                    altitudeLineChart.horizontalGridStep = 9;
                    altitudeLineChart.valueLabelPosition = ValueLabelLeft;
                    altitudeLineChart.labelForIndex = ^(NSUInteger item) {
                        switch (item) {
                            case 0:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:0] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                            }
                                break;
                            case 1:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:1] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 2:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:2] intValue]];
                                    }
                                        break;
                                    default:
                                        break;
                                }
                                
                            }
                                break;
                            case 3:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:3] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 4:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:4] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                            }
                                break;
                            case 5:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:5] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 6:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:6] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 7:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                         return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:7] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 8:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                        return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:8] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                            case 9:
                            {
                                switch (self.segIndex) {
                                    case 0:
                                    {
                                         return [NSString stringWithFormat:@"%d日",[[staticDaysArr objectAtIndex:9] intValue]];
                                    }
                                        break;
                                        default:
                                        break;
                                }
                                
                            }
                                break;
                                
                            default:
                                break;
                        }
                        return @"yadea";
                    };
                    
                    altitudeLineChart.labelForValue = ^(CGFloat value) {
                        return [NSString stringWithFormat:@"%.f", value];
                    };
                    
                    [altitudeLineChart setChartData:chartData];
                    
                }
                    break;
                    
                default:
                    break;
            }
            
            return staticCell;
        }else{
            //历史统计
            AnalysisHistoryTableViewCell *cellNa = [[[NSBundle mainBundle] loadNibNamed:@"AnalysisHistoryTableViewCell" owner:self options:nil] objectAtIndex:0];
            cellNa.selectionStyle=UITableViewCellSelectionStyleNone;

            if ([isBindCars isEqualToString:@"0"])
            {

                PallaTrip *trip =[_tripsArr objectAtIndex:indexPath.row];
                cellNa.startTimeLabel.text = [NSString stringWithFormat:@"开始时间: %@",trip.begintimestr];
                //trip.begintimestr;
                cellNa.endtimeLabel.text =[NSString stringWithFormat:@"结束时间: %@",trip.endtimestr];
                //trip.endtimestr;
                CGFloat distance = [trip.distance doubleValue];
                
                cellNa.milesLabel.text =[NSString stringWithFormat:@"里程: %.2fkm",distance*0.001];
                //trip.distance;
                cellNa.averageSpeedLabel.text =[NSString stringWithFormat:@"平均速度: %@",trip.speed];
                //trip.speed;
            }
            else if ([isBindCars isEqualToString:@"1"])
            {

                PallaTrip *trip =[_tripsArr objectAtIndex:indexPath.row];
                cellNa.startTimeLabel.text = [NSString stringWithFormat:@"开始时间: %@",trip.begintimestr];
                //trip.begintimestr;
                cellNa.endtimeLabel.text =[NSString stringWithFormat:@"结束时间: %@",trip.endtimestr];
                //trip.endtimestr;
                CGFloat distance = [trip.distance doubleValue];
                
                cellNa.milesLabel.text =[NSString stringWithFormat:@"里程: %.2fkm",distance*0.001];
                //trip.distance;
                cellNa.averageSpeedLabel.text =[NSString stringWithFormat:@"平均速度: %@",trip.speed];
                //trip.speed;
            }
            
            return cellNa;
        }
    }
    return cell;

}

-(void)segAction:(UISegmentedControl *)seg
{
    self.segIndex = seg.selectedSegmentIndex;
    if (self.segIndex == 0)
    {
 
        [baseTableview reloadData];
    }
    if (seg.selectedSegmentIndex == 1)
    {
        if (_tripsArr.count == 0) {
            //网络请求行程数据
            [self requestTrips];
        }
        
        if (self.vertiCalDaysDic != nil)
        {
            [baseTableview reloadData];
        }
        else
        {
            if ([isBindCars isEqualToString:@"0"])
            {
                [baseTableview reloadData];
            }
            else if ([isBindCars isEqualToString:@"1"])
            {
                
            }
        }
    }
//    if (seg.selectedSegmentIndex == 2)
//    {
//        if (_tripsArr.count == 0)
//        {
//            if ([isBindCars isEqualToString:@"0"])
//            {
//                [baseTableview reloadData];
//            }
//            else if ([isBindCars isEqualToString:@"1"])
//            {
//                            }
//        }
//        else
//        {
//            [baseTableview reloadData];
//        }
//        
//    }
}


#pragma mark ----时间转化
-(NSString *)transformTimeWithStr:(NSString *)timeStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeStr intValue]];
    NSString*confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.segIndex==1) {

        HistoryTripsViewController *bikeDetect =  [[UIStoryboard storyboardWithName:@"Tab1" bundle:nil] instantiateViewControllerWithIdentifier:@"histroryVc"];

        UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
        temporaryBarButtonItem.title = @"";
        self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
        bikeDetect.mileString = @"5.82km";
        bikeDetect.spedString = @"27km/h";
        bikeDetect.votageString = @"2.5L/100km";

        bikeDetect.tripDb = [[PallaTrip alloc] init];
        bikeDetect.tripDb = [_tripsArr objectAtIndex:indexPath.row];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [paths objectAtIndex:0];
        
        NSString *accountIdString = [[NSUserDefaults standardUserDefaults] valueForKey:@"pallaAccountId"];
        
        
        NSString *jsonFolder = [path stringByAppendingPathComponent:accountIdString];
        
        oldArchive = [ZZArchive archiveWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.zip",jsonFolder,bikeDetect.tripDb.tripId]] error:nil];

        
        firstArchiveEntry = oldArchive.entries[0];
        NSData *newData = [firstArchiveEntry newDataWithError:nil];
        
        if (newData.length !=0) {
            id jsonObj = [NSJSONSerialization JSONObjectWithData:newData options:NSJSONReadingMutableContainers error:nil];
            bikeDetect.pointSArr = [jsonObj valueForKey:@"points"];
            //NSLog(@"****:%@",bikeDetect.pointSArr);
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:bikeDetect animated:YES];
        self.hidesBottomBarWhenPushed = NO;
        
    }
}
}

@end
